#include "Types.h"
#include "GameManager.h"
#include "MemoryStream.h"
#include "GridSceneNode.h"
#include "Ship.h"
#include "Weapon.h"
#include "Projectile.h"
#include "Entity.h"


#include <NxPhysics.h>

char sceneNodeXmlTest[] = "<object speed=\"100\" acceleration=\"200\" heading=\"1,2,3\" position=\"3,4,5\" />";

f32 timer = 0;

void TestUpdate(CGameManager *engine, f32 frameDelta) {

	OBJINTMAP objList = engine->getObjects();
	CSceneObject *obj;
	CShip *shipPtr;

	for(OBJINTMAP::iterator iter = objList.begin();iter != objList.end();iter++) {
		obj = iter->second;

		if(obj->getType() != E_GAMEOBJECT_TYPE::Ship)
			continue;
		
		shipPtr = (CShip *)obj;
	}

	// wait 10 seconds and pause
	timer += frameDelta;
	
	

	//DEBUG_PRINT("%f\n", timer);
	static bool do_once = false;
	if(!do_once) {
//		shipPtr->setAcceleration(shipPtr->getMaxNormalAcceleration());
		do_once = true;
	}
	
	static bool do_twice = false;
	if(!do_twice && timer > 5) {
		//shipPtr->turn(180);

		do_twice = true;
	}

	if(timer > 5) {
//		vector3df speed  = vector3df(0,0,100);

//		mat.setRotationDegrees(shipPtr->getRotation());
//		mat.translateVect(speed);

//		shipPtr->getPhysxObject()->addForce(speed);
		//matrix4 mat;
		//vector3df vel;
		// lets try to get the speed?
		//mat.setRotationDegrees(shipPtr->getVelocity());
		//mat.setRotationDegrees(shipPtr->getVelocity());

		
		//vel = vector3df(0,0,-10000);
		//mat.setRotationDegrees(shipPtr->getVelocity());
		//mat.transformVect(vel);
		//shipPtr->getPhysxObject()->addForce(vel);
			

	}

		
}

void TestLoad(CGameManager *engine) {
	ISceneManager *mgr;
	CGridSceneNode *xy, *z;
	CShip *obj;
	int x;
	char temp[1024];
	CShip *ptr;

	// create instance for reference
	CEntity *blah = new CEntity();


	mgr = engine->getSceneManager();

	IMesh* mesh = mgr->getMesh("Content\\TestNike.ms3d");
	
	CProjectile *proj = new CProjectile();
	OBJSTRMAP ships = CObjectTypesContainer::Ships;

	for(OBJSTRMAP::iterator iter = ships.begin(); iter !=ships.end();iter++) {
		if(!stricmp("SHIP_TEST", iter->second->getXmlID())) {
			ptr = (CShip *)iter->second;
			break;
		}
	}
	
	for(x=1;x<=2;x++) {
		sprintf_s(temp, 1024, "Content\\xml\\wtf1.xml", x);
		obj = ptr->copy();
		//obj->setSceneNode(mgr->addMeshSceneNode(mesh));
		//obj->setSceneNode(mgr->addCubeSceneNode(10));

		obj->getSceneNode()->getMaterial(0).EmissiveColor = SColor(0,255,0,0); 
		
		//obj->setPhysxObject( engine->getPhysxManager()->createBoxObject(vector3df(0,0,0), vector3df(0,0,0), vector3df(0.5f,0.5f,0.5f)) );
		//IPhysxMesh *physxMesh = engine->getPhysxManager()->createConvexMesh(mesh->getMeshBuffer(0), vector3df(1,1,1));
		//obj->setPhysxObject(engine->getPhysxManager()->createConvexMeshObject(physxMesh, vector3df(0,0,0)));
		obj->deserialize(temp);
		obj->setPosition(vector3df(x*1000.0f,0.0f,(x*3000.0f)));

		
		//DEBUG_PRINT("Speed:[%f]\tAccel:[%f]", obj->getSpeed(), obj->getAcceleration());
		//obj->getVelocity().getAs4Values(t);
		//DEBUG_PRINT("Heading: [%f]\t[%f]\t[%f]\t[%f]\n", t[0],t[1],t[2],t[3]);
		//obj->getPosition().getAs4Values(t);
		//DEBUG_PRINT("Position: [%f]\t[%f]\t[%f]\t[%f]\n", t[0],t[1],t[2],t[3]);
		
		engine->addObject((CSceneObject *)obj);
	}
	


	DEBUG_PRINT("\n\n------\nShip objects Count:\t%d\n------\n", CShip::getInstanceCount()-engine->getShipTypes().size());
	DEBUG_PRINT("------\nPhysx objects Count:\t%d\n------\n", engine->getPhysxManager()->getNumPhysxObjects());
	xy = new scene::CGridSceneNode(mgr->getRootSceneNode(), mgr);
	z = new scene::CGridSceneNode(mgr->getRootSceneNode(), mgr);
	z->setRotation(vector3df(90,0,0));
	//xy->drop(); 
	//z->drop();
}

void TestMove(CGameManager *engine, f32 frameDeltaTime) {
	//f32 movement;

	//movement = 10 * frameDeltaTime;
	//obj->move(vector3df(0,0,movement));
}